<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Get all users
 * @returns array
 * @return array of zero or more users, or NULL on failure
 * @param uid integer
 * @param tagcount integer
 * @param postcount integer
 * @param timestamp
 */

function conversations_userapi_getusers($args)
{
    extract($args);

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $cusers = $xartable['cusers'];

    $query = "SELECT DISTINCT xar_uid, xar_tagcount, xar_postcount, xar_timestamp, "
            . " xar_options "
            . " FROM $cusers";

    $where = array();
    $bind = array();

    if (!empty($uid)) {
        $where[] = 'xar_uid = ?';
        $bind[] = (int)$uid;
    }

    if (!empty($tagcount)) {
        $where[] = 'xar_tagcount > ?';
        $bind[] = (int)$tagcount;
    }

    if (!empty($postcount)) {
        $where[] = 'xar_postcount > ?';
        $bind[] = (int)$postcount;
    }
    
    if (!empty($timestamp)) {
        $where[] = 'xar_timestamp > ?';
        $bind[] = (int)$timestamp;
    }

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY xar_uid";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $users = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($uid, $tagcount, $postcount, $timestamp, $options) = $result->fields;

        $users[] = array(
                'uid'       => $uid,
                'tagcount'  => $tagcount,
                'postcount' => $postcount,
                'timestamp' => $timestamp//,
                //'options'   => unserialize($options)
        );
    }
    $result->Close();
    return $users;
}
?>